var laydate;
var $;
$(function(){
    layui.use('laydate', function() {
        laydate = layui.laydate;
        $ = layui.$;

        //调用统计昨日订单数和昨日销售额度方法
        getYesterdayOrder();
        //调用统计全部订单数和销售总额方法
        getAllOrders();
        //调用统计待审核课程方法
        getAuditCourses();
        //统计待审核商品
        getAuditProduct();
        //统计时间段内订单数
        getTimeOrder();
        //设置统计图默认日期
        getDefaultDate();
        //统计某天订单数 百分比
        getOrdersNumber();
        //日期插件——获取指定日期的统计图
        laydate.render({
            elem: '#test1',
            done: function (value) {
                $.post("/OpeHome/getSpecifyDate",{'SpecifyDate':value}, function (data) {
                    StatisticalFigure(data);
                });

                var max;
                var yesterday = getNowFormatDate(value,-1,"-");
                $.post("/OpeHome/getOrdersNumber",{'yesterday':yesterday,'today':value}, function (data) {
                    $("#p_2").text(data.obj);
                    max=data.obj;
                });

                var yesterday1 = getNowFormatDate(value,-2,"-");
                $.post("/OpeHome/getOrdersNumber",{'yesterday':yesterday1,'today':yesterday}, function (data) {
                    $("#p_2_1").text(data.obj);
                    var p=Math.round((max/data.obj)*10000/100)+"%";
                    $('#p_3').text('▲'+p);
                });
            }
        })
    });
});
//统计昨日订单数和昨日销售额度
function getYesterdayOrder(){
    $.get("/OpeHome/getYesterdayOrder", function (data) {
        $("#YesterdayOrde").text(data.obj[0].YesterdayOrde);
        $("#YesterdayAmount").text("￥"+data.obj[0].YesterdayAmount);
    });
}

//统计全部订单数和销售总额
function getAllOrders(){
    $.get("/OpeHome/getAllOrders", function (data) {
        $("#AllOrders").text(data.obj[0].AllAmount);
        $("#AllAmount").text("￥"+data.obj[0].AllOrders);
    });
}

//统计待审核课程
function getAuditCourses(){
    $.post("/OpeHome/getAuditCourses",{"AuditCourses":3}, function (data) {
        $("#AuditCourses").text(data.obj[0].AuditCourses);
        if(data.obj[0].AuditCourses==0){
            $("#li1").css("display","none");
        }else{
            $("#li1").css("display","block");
        };
    });
}
//统计待审核商品
function getAuditProduct(){
    $.post("/OpeHome/getAuditProduct",{"AuditProduct":2}, function (data) {
        $("#AuditProduct").text(data.obj[0].AuditProduct);
        if(data.obj[0].AuditProduct==0){
            $("#li2").css("display","none");
        }else{
            $("#li2").css("display","block");
        }
    });
}
//统计某天订单数 百分比
function getOrdersNumber(){
    var max;
    var yesterday = getDay(-1, '-');
    var today = getDay(0, '-');
    $.post("/OpeHome/getOrdersNumber",{'yesterday':yesterday,'today':today}, function (data) {
        $("#p_2").text(data.obj);
        max=data.obj;
        console.log(max);
    });

    var yesterday1 = getDay(-2, '-');
    $.post("/OpeHome/getOrdersNumber",{'yesterday':yesterday1,'today':yesterday}, function (data) {
        $("#p_2_1").text(data.obj);
        console.log(max);
        console.log(data.obj);
        var p=Math.round((max/data.obj)*10000/100)+"%";
        $('#p_3').text('▲'+p);
    });
}
//统计时间段内订单数——统计图
function getTimeOrder(){
    var yesterday = getDay(-1, '-');
    $.post("/OpeHome/getSpecifyDate", {'SpecifyDate':yesterday}, function (data) {
        StatisticalFigure(data);
    });
}
//设置统计图默认日期
function getDefaultDate(){
    var yesterday = getDay(-1, '-');
    $("#test1").val(yesterday);
}

//刷新
$("#refreshs").on('click',function(){
    window.location.reload();
})
//获取前一日日期
function getNowFormatDate(sdate,interval,caret) {
    var patt1 =/^\d{4}-([0-1]?[0-9])-([0-3]?[0-9])$/;  //判断输入的日期是否符合格式正则表达式
    if(!(sdate && typeof(sdate) == "string" &&  patt1.test(sdate))){
        sdate = new Date(); //不满足日期的则使用当前年月日
    }
    interval = isNaN(parseInt(interval)) ? 0 : parseInt(interval);//若没有输入间隔，则使用当前日
    caret = (caret && typeof(caret) == "string") ? caret : "";
    var gdate = new Date(sdate).getTime();//获取指定年月日
    gdate = gdate + 1000*60*60*24*interval; //加减相差毫秒数
    var speDate = new Date(gdate);//获取指定好毫秒数时间
    var preYear = speDate.getFullYear();
    var preMonth = speDate.getMonth() + 1;
    var preDay = speDate.getDate();
    preMonth = (preMonth < 10) ? ("0" + preMonth) :preMonth;
    preDay = (preDay < 10) ? ("0" + preDay) :preDay;
    var preDate =  preYear + caret +  preMonth + caret + preDay;
    return preDate;
};

function getDay(num, str) {
    var today = new Date();
    var nowTime = today.getTime();
    var ms = 24*3600*1000*num;
    today.setTime(parseInt(nowTime + ms));
    var oYear = today.getFullYear();
    var oMoth = (today.getMonth() + 1).toString();
    if (oMoth.length <= 1) oMoth = '0' + oMoth;
    var oDay = today.getDate().toString();
    if (oDay.length <= 1) oDay = '0' + oDay;
    return oYear + str + oMoth + str + oDay;
}
//封装统计图
function StatisticalFigure(data){
    var chartDom = document.getElementById('main');
    var myChart = echarts.init(chartDom);
    // 指定图表的配置项和数据
    var option = {
        title: {
            text: "订单统计",
            left: '7%',
        },
        grid: {
            left: '10%',
            right: '10%',
            bottom: '10%',
            containLabel: true
        },
        xAxis: {
            data: ["0.00~3.00","3.00~6.00","6.00~9.00","9.00~12.00","12.00~15.00","15.00~18.00","18.00~21.00","21.00~0.00"]
        },
        yAxis: {
            type: 'value',
            min:0,
            max:200,
            interval:50,
        },
        series: [
            {
                name: '销量',
                type: 'bar',
                barWidth: '50%',
                data: [data.obj[0],data.obj[1],data.obj[2],data.obj[3],
                    data.obj[4],data.obj[5],data.obj[6],data.obj[7]]
            }
        ]
    };
    option && myChart.setOption(option);
}